package com.ddone.ioc.xml.factory;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;

/**
 * @author ddone
 * @date 2024/3/19-17:46
 */
@Component
public class MybatisFactoryBean implements FactoryBean<SqlSessionFactory> {

    @Value("mybatis-config.xml")
    private String config;

    @Override
    public SqlSessionFactory getObject() throws Exception {
        InputStream asStream = Resources.getResourceAsStream(config);
        return new SqlSessionFactoryBuilder().build(asStream);
    }

    @Override
    public Class<?> getObjectType() {
        return SqlSessionFactory.class;
    }
}
